package com.jswl.training.springjpa.aspect;


import com.jswl.training.springjpa.annotation.Action;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class ActionAspect {

    @Pointcut("@annotation(com.jswl.training.springjpa.annotation.Action)")
    public void annotationPointCut(){

    }

    @After("@annotation(controllerAction)")
    public void after(JoinPoint joinPoint, Action controllerAction){
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("注解式拦截:"+ controllerAction.name());
    }

    @Before("execution(* com.jswl.training.springjpa.controller.*.*(..))")
    public void before(JoinPoint joinpoint){
        MethodSignature signature = (MethodSignature)joinpoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则拦截："+method.getName());
    }

//    @Around("annotationPointCut()")
//    public void around(JoinPoint joinPoint){
//        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
//        Method method = signature.getMethod();
//        Action annotation = method.getAnnotation(Action.class);
//        System.out.println("------:"+annotation.name());
//
//    }
}
